package com.earthea.application.web.controller.monitor;

import com.earthea.application.common.annotation.Log;
import com.earthea.application.common.core.controller.BaseController;
import com.earthea.application.common.entity.BaseResult;
import com.earthea.application.common.entity.TableDataInfo;
import com.earthea.application.common.enums.BusinessType;
import com.earthea.application.common.utils.poi.ExcelUtil;
import com.earthea.application.system.domain.entity.SysOperLog;
import com.earthea.application.system.domain.service.ISysOperLogService;
import com.earthea.application.system.entity.request.SysOperLogReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 */
@RestController
@RequestMapping("/rest/monitor/operlog")
public class SysOperLogController extends BaseController {
    @Autowired
    private ISysOperLogService operLogService;

    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public TableDataInfo<SysOperLog> list(SysOperLogReq req) {
        return operLogService.select(req);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogReq req) {
        TableDataInfo<SysOperLog> info = operLogService.select(req);
        ExcelUtil.export(response, "操作日志", info.getRows(), SysOperLog.class);
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public BaseResult remove(@PathVariable List<Long> operIds) {
        return toResult(operLogService.batchDelete(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public BaseResult clean() {
        operLogService.clean();
        return success();
    }
}
